package com.guinea.account.service;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.util.Assert;

import com.guinea.account.dao.UserDao;
import com.guinea.account.entity.Role;
import com.guinea.account.entity.User;
import com.guinea.core.acegi.cache.AcegiCacheManager;
import com.guinea.core.orm.hibernate.HibernateDao;
import com.guinea.core.service.CRUDManager;
import com.guinea.svw.dao.SvwJdbcDao;
import com.guinea.svw.entity.SvwUser;

/**
 * @author huaxiaz
 */
public class UserManager extends CRUDManager {
	private AcegiCacheManager acegiCacheManager;
	SvwJdbcDao svwJdbcDao;
	private UserDao userDao;

	public HibernateDao getEntityDao() {
		return getUserDao();
	}

	public User getUser(Serializable id) {
		return (User)getUserDao().get(id);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public SvwJdbcDao getSvwJdbcDao() {
		return svwJdbcDao;
	}

	public void setSvwJdbcDao(SvwJdbcDao svwAccountDao) {
		this.svwJdbcDao = svwAccountDao;
	}
	
	public void setAcegiCacheManager(AcegiCacheManager acegiCacheManager) {
		this.acegiCacheManager = acegiCacheManager;
	}
	
	public void saveUser(User user){
		Serializable id = user.getId();
		String originName = null;
		if(id != null)
			originName = getUser(id).getLoginName();
		getUserDao().save(user);
		if(originName == null)
			originName = user.getLoginName();
		acegiCacheManager.modifyUserInCache(user,originName);
	}
	//标记删除
	public void eraseUser(User user){
		getUserDao().save(user);
		String originName = user.getLoginName();
		if(originName != null)
			acegiCacheManager.getUserCache().removeUserFromCache(originName);
	}
	
	public void deleteUser(User user){
		String originName = null;
		if(user != null)
			originName = user.getLoginName();
		getUserDao().delete(user);
		if(originName != null)
			acegiCacheManager.getUserCache().removeUserFromCache(originName);
	}
	
    public String[] getRelativeRoleIds(Serializable id) {
        Role role;
        User user = getUser(id);
        Set roles = user.getRoles();
        String[] relativeRoleIds = new String[roles.size()];
        int i = 0;
        for (java.util.Iterator iter = roles.iterator(); iter.hasNext();) {
            role = (Role) iter.next();
            Assert.notNull(role.getId());
            relativeRoleIds[i] = String.valueOf(role.getId().longValue());
            i++;
        }
        return relativeRoleIds;
    }
    
    public List getUsersByName(String name){
    	return getUserDao().find("from User where  status='1' and loginName=?", name);
    }
    public void initUsers(){
		List users = (List)getUserDao().find("from User where originSystem = 1");
		List svwUsers = getSvwJdbcDao().getAllUsers();
    	for (Iterator iter = svwUsers.iterator(); iter.hasNext();) {
			SvwUser svwUser = (SvwUser) iter.next();
			Assert.notNull(svwUser.getUserId());
			User user = null;
			for (Iterator iterator = users.iterator(); iterator.hasNext();) {
				User u = (User) iterator.next();
				if (u.getOriginId() != null && u.getOriginId().longValue() == svwUser.getUserId().longValue()) {
					user = u;
					break;
				}
			}
			if(user == null){
				user = new User();
				user.setOriginId(svwUser.getUserId());
				//skodx
				user.setOriginSystem("SKODX");
			}
			user.setOriginCode(svwUser.getGroupCode());
			user.setLoginName(svwUser.getLoginName());
			user.setLoginPassword(svwUser.getPassword());
			user.setOtherName(svwUser.getEmployName());
			getUserDao().save(user);
		}
    }
}
